// 设置页面逻辑
Page({
  data: {
    settings: {
      dailyReminder: false,
      reminderTime: '20:00',
      autoShowAnswer: true,
      difficulty: 'all',
      difficultyText: '全部难度',
      darkMode: false,
      fontSize: 'normal',
      fontSizeText: '标准',
      animation: true
    },
    version: '1.0.0'
  },

  onLoad: function (options) {
    this.loadSettings();
  },

  // 加载设置
  loadSettings: function () {
    const settings = wx.getStorageSync('settings') || this.data.settings;
    this.setData({ settings: settings });
  },

  // 保存设置
  saveSettings: function () {
    wx.setStorageSync('settings', this.data.settings);
  },

  // 切换每日提醒
  toggleDailyReminder: function (e) {
    const value = e.detail.value;
    this.setData({
      'settings.dailyReminder': value
    });
    this.saveSettings();
    
    if (value) {
      this.requestNotificationPermission();
    }
  },

  // 请求通知权限
  requestNotificationPermission: function () {
    wx.requestSubscribeMessage({
      tmplIds: ['your-template-id'],
      success: (res) => {
        console.log('通知权限请求结果:', res);
      }
    });
  },

  // 设置提醒时间
  setReminderTime: function () {
    wx.showTimePickerView({
      current: this.data.settings.reminderTime,
      success: (res) => {
        this.setData({
          'settings.reminderTime': res.time
        });
        this.saveSettings();
      }
    });
  },

  // 切换自动显示答案
  toggleAutoShowAnswer: function (e) {
    const value = e.detail.value;
    this.setData({
      'settings.autoShowAnswer': value
    });
    this.saveSettings();
  },

  // 设置难度
  setDifficulty: function () {
    const difficulties = [
      { value: 'all', text: '全部难度' },
      { value: 'easy', text: '简单' },
      { value: 'medium', text: '中等' },
      { value: 'hard', text: '困难' }
    ];

    wx.showActionSheet({
      itemList: difficulties.map(d => d.text),
      success: (res) => {
        const selected = difficulties[res.tapIndex];
        this.setData({
          'settings.difficulty': selected.value,
          'settings.difficultyText': selected.text
        });
        this.saveSettings();
      }
    });
  },

  // 切换深色模式
  toggleDarkMode: function (e) {
    const value = e.detail.value;
    this.setData({
      'settings.darkMode': value
    });
    this.saveSettings();
    
    // 应用主题
    this.applyTheme(value);
  },

  // 应用主题
  applyTheme: function (isDark) {
    // 这里可以实现主题切换逻辑
    console.log('切换主题:', isDark ? '深色' : '浅色');
  },

  // 设置字体大小
  setFontSize: function () {
    const fontSizes = [
      { value: 'small', text: '小' },
      { value: 'normal', text: '标准' },
      { value: 'large', text: '大' }
    ];

    wx.showActionSheet({
      itemList: fontSizes.map(f => f.text),
      success: (res) => {
        const selected = fontSizes[res.tapIndex];
        this.setData({
          'settings.fontSize': selected.value,
          'settings.fontSizeText': selected.text
        });
        this.saveSettings();
      }
    });
  },

  // 切换动画效果
  toggleAnimation: function (e) {
    const value = e.detail.value;
    this.setData({
      'settings.animation': value
    });
    this.saveSettings();
  },

  // 导出数据
  exportData: function () {
    const data = {
      settings: this.data.settings,
      userInfo: wx.getStorageSync('userInfo'),
      studyHistory: wx.getStorageSync('studyHistory'),
      wrongQuestions: wx.getStorageSync('wrongQuestions'),
      favorites: wx.getStorageSync('favorites'),
      exportTime: new Date().toISOString()
    };

    // 模拟导出功能
    wx.showModal({
      title: '导出数据',
      content: '数据已准备就绪，请复制以下内容保存到本地文件',
      showCancel: false,
      success: () => {
        wx.setClipboardData({
          data: JSON.stringify(data, null, 2),
          success: () => {
            wx.showToast({
              title: '数据已复制到剪贴板',
              icon: 'success'
            });
          }
        });
      }
    });
  },

  // 导入数据
  importData: function () {
    wx.showModal({
      title: '导入数据',
      content: '请将之前导出的数据粘贴到输入框中',
      showCancel: true,
      success: (res) => {
        if (res.confirm) {
          // 这里可以实现数据导入逻辑
          wx.showToast({
            title: '导入功能开发中',
            icon: 'none'
          });
        }
      }
    });
  },

  // 清空数据
  clearData: function () {
    wx.showModal({
      title: '确认清空',
      content: '确定要清空所有数据吗？此操作不可恢复。',
      success: (res) => {
        if (res.confirm) {
          wx.clearStorageSync();
          this.loadSettings();
          
          wx.showToast({
            title: '数据已清空',
            icon: 'success'
          });
        }
      }
    });
  },

  // 检查更新
  checkUpdate: function () {
    const updateManager = wx.getUpdateManager();
    
    updateManager.onCheckForUpdate((res) => {
      if (res.hasUpdate) {
        wx.showModal({
          title: '发现新版本',
          content: '是否立即更新？',
          success: (res) => {
            if (res.confirm) {
              updateManager.applyUpdate();
            }
          }
        });
      } else {
        wx.showToast({
          title: '已是最新版本',
          icon: 'success'
        });
      }
    });
  },

  // 跳转到意见反馈
  goToFeedback: function () {
    wx.navigateTo({
      url: '/pages/feedback/feedback'
    });
  },

  // 跳转到关于我们
  goToAbout: function () {
    wx.navigateTo({
      url: '/pages/about/about'
    });
  },

  // 重置设置
  resetSettings: function () {
    wx.showModal({
      title: '确认重置',
      content: '确定要恢复默认设置吗？',
      success: (res) => {
        if (res.confirm) {
          const defaultSettings = {
            dailyReminder: false,
            reminderTime: '20:00',
            autoShowAnswer: true,
            difficulty: 'all',
            difficultyText: '全部难度',
            darkMode: false,
            fontSize: 'normal',
            fontSizeText: '标准',
            animation: true
          };
          
          this.setData({ settings: defaultSettings });
          this.saveSettings();
          
          wx.showToast({
            title: '设置已重置',
            icon: 'success'
          });
        }
      }
    });
  }
}); 